﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LogLib;

namespace ComLib.ComFun
{
    public class FileOperation
    {
        /// <summary>
        /// 删除指定目录中的所有照片
        /// </summary>
        /// <param name="srcPath"></param>
        public static void DeleteAllImagesInDir(string srcPath)
        {
            if(!Directory.Exists(srcPath))
            {
                return;
            }
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (!(i is DirectoryInfo))
                    {
                        string ext = Path.GetExtension(i.FullName);
                        if (ext.Equals(".jpg") || ext.Equals(".bmp") || ext.Equals(".png"))
                        {
                            File.Delete(i.FullName);      //删除指定文件
                            Log.WriteAsync("已删除: " + i.FullName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }
        }

        /// <summary>
        /// 删除指定目录中的所有Execl
        /// </summary>
        /// <param name="srcPath"></param>
        public static void DeleteAllExeclsInDir(string srcPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (!(i is DirectoryInfo))
                    {
                        string ext = Path.GetExtension(i.FullName);
                        if (ext.Equals(".xls"))
                        {
                            File.Delete(i.FullName);      //删除指定文件
                            Log.WriteAsync("已删除: " + i.FullName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }
        }
    }
}
